//
//  THJKindVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/9.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJKindVC.h"

@interface THJKindVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableview;
    NSArray *_titleArr;
}
@end

@implementation THJKindVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setData];
    [self creatTableview];
    self.title = @"分类";
    
    // Do any additional setup after loading the view from its nib.
}
-(void)setData{
    
     NSString * parentid;
    if ([self.typeStr isEqual:@"硅系"]) {
        parentid = @"73667";
       // _titleArr = @[@"硅铁",@"金属硅",@"碳化硅",@"晶体硅",@"有机硅",@"硅石"];
    }else if([self.typeStr isEqual:@"锰系"]){
        parentid = @"73668";
        //_titleArr = @[@"硅锰",@"锰铁",@"金属锰",@"锰矿",@"富锰渣",@"锰化工"];
    }else if([self.typeStr isEqual:@"铬系"]){
        parentid = @"73669";
        //_titleArr = @[@"铬铁",@"铬矿",@"氮化铬铁",@"金属铬铁",@"金属铬",@"硅铬",@"铬盐"];
    }else if([self.typeStr isEqual:@"铝系"]){
        parentid = @"73670";
        //_titleArr = @[@"硅铝铁",@"铝铁",@"铝锰矿",@"铝锭",@"铝条"];
    }else if([self.typeStr isEqual:@"稀有金属"]){
        parentid = @"73676";
        //_titleArr = @[@"钒",@"钛",@"钨",@"钼",@"镍",@"镁",@"铅锌",@"锆",@"旦",@"钾"];
    }else if([self.typeStr isEqual:@"多元合金"]){
        parentid = @"75156";
        //_titleArr = @[@"硅钙",@"硅铝",@"硅铬",@"硅铝钡钙",@"硅铝钙",@"硅铝钡",@"铝硅锰",@"稀土硅镁合金",@"硅钡"];
    }else if([self.typeStr isEqual:@"炉料"]){
        parentid = @"73676";
        //_titleArr = @[@"硅锰球",@"硅铁球",@"锰碳球",@"硅碳球",@"硅铝球"];
    }else{
        if([self.istg isEqual:@"团购"]){
            [MBProgressHUD showMessage:@"正在加载" toView:self.view];
            __weak typeof(self) weakself = self;
            NSString * urlStr = [NSString stringWithFormat:@"%@%@%@",THJDomain,THJGroupPHP,@"action=catlist"];
            [THJNetWork getNetDataFromUrl:urlStr callback:^(NSDictionary* receiveData, NSError *error) {
                NSLog(@"种类--%@",receiveData);
                [MBProgressHUD hideHUDForView:weakself.view animated:YES];
                if([receiveData[@"status"] isEqual:@"success"]){
                     _titleArr = receiveData[@"list"];
                    [_tableview reloadData];
                }else{
                    [weakself HUDShowMsg:@"获取失败" AfterDelayDimiss:1];
                }
            }];
        }else if([self.typeStr isEqual:@"招标"]){
            _titleArr = @[@"硅系",@"锰系",@"铬系",@"稀有金属",@"多元合金",@"炉料"];
        }else{
            _titleArr = @[@"硅系",@"锰系",@"铬系",@"铝系",@"稀有金属",@"多元合金",@"炉料"];
        }
    }
    if(![self.typeStr isEqual:@"分类"]&&![self.typeStr isEqual:@"招标"]){
        NSLog(@"MMMMMMMMMMMMMMMMMM");
        [MBProgressHUD showMessage:@"正在加载" toView:self.view];
        NSString * urlStr = [NSString stringWithFormat:@"%@%@%@%@%@",THJDomain,THJStorePHP,@"action=productgroup&moduleid=31&parentid=",parentid,@"&level=2"];
        NSLog(@"种类%@",urlStr);
        __weak typeof(self) weakself = self;
        [THJNetWork getCompanyIntroFromUrl:urlStr callback:^(NSDictionary* receiveData, NSError *error) {
            NSLog(@"种类---%@",receiveData);
            [MBProgressHUD hideHUDForView:weakself.view];
            if([receiveData[@"state"] isEqual:@"success"]){
                 _titleArr = receiveData[@"data"];
                //  [self HUDShowMsg:receiveData[@"message"] AfterDelayDimiss:1];
                [_tableview reloadData];
            }else{
                [self HUDShowMsg:receiveData[@"message"] AfterDelayDimiss:1];
            }
        }Type:1];
    }
   
}
-(void)creatTableview{
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    _tableview.tableFooterView = [[UIView alloc] init];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    if([self.typeStr isEqual:@"分类"] || [self.typeStr isEqual:@"招标"]){
        
        if ([self.istg isEqual:@"团购"]) {
            NSDictionary * dic = _titleArr[indexPath.row];
            cell.textLabel.text = dic[@"catname"];
        }else{
            cell.textLabel.text = _titleArr[indexPath.row];
        }
    }else{
        cell.textLabel.text = _titleArr[indexPath.row][@"text"];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.typeStr isEqual:@"分类"]||[self.typeStr isEqual:@"招标"]) {
        if ([self.istg isEqual:@"团购"]) {
            NSDictionary * dic = _titleArr[indexPath.row];
             if ([self.delegate respondsToSelector:@selector(cellClickedWithKind:Value:)]){
                 [self.delegate cellClickedWithKind:dic[@"catname"] Value:dic[@"catid"]];
             }
        }else{
            if ([self.delegate respondsToSelector:@selector(cellClickedWithKind:Value:)]){
                [self.delegate cellClickedWithKind:_titleArr[indexPath.row] Value:nil];
            }
        }
        
    }else{
        if ([self.delegate respondsToSelector:@selector(cellClickedWithKind:Value:)]){
            [self.delegate cellClickedWithKind:_titleArr[indexPath.row][@"text"] Value:_titleArr[indexPath.row][@"value"]];
        }
    }
 }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
